package com.fessc.develop.handler;

import com.fessc.develop.entity.dbvo.SettleTrade;
import com.fessc.develop.exception.BusinessException;
import com.fessc.develop.handler.processor.send.CCBISendProcessor;
import com.fessc.develop.model.enums.EnumBranch;
import com.fessc.develop.utils.BusiUtil;
import com.fessc.develop.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CCBSettleTradePushHandler {

    @Resource
    private List<CCBISendProcessor> orderApplyProcessors;

    private Map<String, CCBISendProcessor> processorMap;

    @PostConstruct
    public void init() {
        if (BusiUtil.isNotNull(orderApplyProcessors)) {
            processorMap = orderApplyProcessors.stream().collect(Collectors.toMap(CCBISendProcessor::getProcessorForBranch, p->p, (v1, v2) -> v1));
        } else {
            processorMap = new HashMap<>();
        }
    }

    /**
     * 建行跨境付汇信息推送
     */
    public void settleTradePush(List<SettleTrade> settleTrades) {
        //按照银行分类处理
        Map<String, List<SettleTrade>> settleTradeMap = settleTrades.stream().collect(Collectors.groupingBy(t -> t.getBankid().toString()));

        //加工构造推送报文，调用接口推送
        settleTradePushProcess(settleTradeMap);
    }

    private void settleTradePushProcess(Map<String, List<SettleTrade>> crossBorderMap) {
        for (Map.Entry<String, List<SettleTrade>> entry : crossBorderMap.entrySet())  {
            try {
                CCBISendProcessor processor = BusiUtil.isNull(processorMap.get(entry.getKey().toString()))? processorMap.get(EnumBranch.CCBBANK.toString()) : processorMap.get(entry.getKey().toString());
                if (BusiUtil.isNull(processor)) {
                    throw ExceptionUtil.businessException("EE4001");
                }
                log.info("推送建行跨境付汇交易");
                processor.settleTradeProcess(entry.getValue()); //跨境付汇信息推送
            } catch (Exception e) {
                if (e instanceof BusinessException) {
                    log.error(((BusinessException) e).getResult().getMsg(), e);
                } else {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    /**
     * 建行跨境付汇状态查询推送
     */
    public void settleTradeStatusPush(List<SettleTrade> settleTrades, String pushType) {
        //按照银行分类处理
        Map<String, List<SettleTrade>> settleTradeMap = settleTrades.stream().collect(Collectors.groupingBy(t -> t.getBankid().toString()));

        //加工构造推送报文，调用接口推送
        settleTradeStatusProcess(settleTradeMap);
    }

    private void settleTradeStatusProcess(Map<String, List<SettleTrade>> settleTradeMap) {
        for (Map.Entry<String, List<SettleTrade>> entry : settleTradeMap.entrySet())  {
            try {
                CCBISendProcessor processor = BusiUtil.isNull(processorMap.get(entry.getKey()))? processorMap.get(EnumBranch.CCBBANK.toString()) : processorMap.get(entry.getKey());
                if (BusiUtil.isNull(processor)) {
                    throw ExceptionUtil.businessException("EE4001");
                }
                log.info("推送建行跨境付汇交易状态查询交易");
                processor.settleTradeStatusProcess(entry.getValue()); //跨境付汇状态信息推送
            } catch (Exception e) {
                if (e instanceof BusinessException) {
                    log.error(((BusinessException) e).getResult().getMsg(), e);
                } else {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

}
